// pages/message/message.js
const {
    imgConfig
} = require('../../utils/config.js')
// pages/message/message.js
const {
    config
} = require('../../utils/config.js')
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: `${imgConfig}`,
        statusBarHeight: app.globalData.statusBarHeight,
        msgData: [],
        page: 1,
        limit: 8,
        focus: false,
        InputBottom: 0,
        stuId: app.globalData.userInfo.id,
        reply:"请输入评论"
    },
    InputFocus(e) {
        this.setData({
          InputBottom: e.detail.height
        })
      },
      InputBlur(e) {
        this.setData({
          InputBottom: 0
        })
      },
    back: function () {
        wx.navigateBack({});
    },
    changeFocus(e){
      this.data.reply =''
      for (let i = 0; i < this.data.msgData.length; i++) {
        if (e.currentTarget.dataset.id == this.data.msgData[i].id) {
          this.data.reply = this.data.msgData[i].studentInfo.stuName
        }
      }
      this.setData({
        discussId:e.target.dataset.id,
        focus:true,
        reply:("回复 "+this.data.reply)
      })
    },
    /**
     * 文本框输入数据绑定
     * @param {*} ev 
     */
    changeInputValue(ev) {
        this.setData({
            inputVal: ev.detail.value
        })
    },
    //删除留言
    DelMsg(e) {
        var that = this
        wx.showModal({
            cancelColor: 'cancelColor',
            title: '是否确认删除?',
            success(res) {
                if (res.confirm) {
                    wx.request({
                        url: `${config}/wechat/wxMessage/delete`,
                        data: {
                            id: e.currentTarget.dataset.item
                        },
                        success(resp) {
                            console.log(resp)
                            wx.showToast({
                                title: '已删除',
                                duration: 1000
                            })
                            that.onLoad()
                        }
                    })
                } else if (res.cancel) {
                    wx.showToast({
                        title: '已取消',
                        icon: 'none',
                        duration: 1000
                    })
                    that.onLoad()
                }
            }
        })
    },
    //添加留言
    addMsg() {
        var that = this
        console.log(that.data.discussId)
        if (that.data.inputVal != "" && that.data.inputVal != null && that.data.inputVal != undefined) {
            wx.request({
                url: `${config}/wechat/wxMessage/saveMessage`,
                method: 'POST',
                data: {
                    stuId: app.globalData.userInfo.id,
                    deptId: app.globalData.userInfo.deptId,
                    deleteStatus: false,
                    messageValue: that.data.inputVal,
                    superiorId: that.data.discussId
                },
                success(resp) {
                    that.onLoad()
                    that.setData({
                        inputVal: ''
                    })
                }
            })
        } else {
            wx.showToast({
                title: '留言不能为空',
                icon: 'none',
                duration: 1000
            })
        }
    },
    /**
     * 获取留言板信息 机构id区分
     */
    getMessageList(){
        wx.showLoading({
          title: '加载中',
        })
        var that = this 
        console.log(that.data);
        console.log("点击评论");
        that.data.page = 1
        wx.request({
            url: `${config}/wechat/wxMessage/messageList`,
            data: {
                page: that.data.page,
                deptId: app.globalData.userInfo.deptId
            },
            success(res) {
                console.log(res)
                that.setData({
                    msgData: res.data.messageList.list
                })
                wx.hideLoading({
                  success: (res) => {},
                })
            }
        })
    },
    /**
     * 分页加载
     */
    pageLimit(){
        var that = this
        if (that.data.msgData != "") {
            wx.showLoading({
                title: '玩命加载中',
                mask: true,
            })
        }
        that.data.page++ //页码自增
        //发起请求
        wx.request({
            url: `${config}/wechat/wxMessage/messageList`,
            data: {
                page: that.data.page,
                limit: that.data.limit,
                deptId: app.globalData.userInfo.deptId,
            },
            success(res) {
                if (res.data.code == 0) {
                    that.setData({
                        //拼接记录数据
                        msgData: that.data.msgData.concat(res.data.messageList.list)
                    })
                }
                // 隐藏加载框
                wx.hideLoading();
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getMessageList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
       this.pageLimit()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})